package com.ruoyi.form.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.DynamicClassInfo;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.form.entity.ClassForm;
import com.ruoyi.form.entity.ConfigurationBuilder;
import com.ruoyi.form.service.ClassFormService;
import com.ruoyi.hov.ClassBindHovUtils;
import com.ruoyi.hov.entity.ClassBindHov;
import com.ruoyi.util.DynamicUtil;
import com.ruoyi.util.SuperClassFilter;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * **********************
 * * Author: XiaShiLong *
 * * Date  : 2021-03-26 *
 * * Time  : 14:47      *
 * * to    : lz&xm      *
 * **********************
 **/
@Service
public class ClassFormServiceImpl implements ClassFormService {

    /**
     * 获取表单
     *
     * @param menuId    菜单id
     * @param classPath 类路径
     * @return List
     */
    @Override
    public List<ClassForm> getForm(Long menuId, String classPath) {
        return build(ConfigurationBuilder.formBuild(menuId, classPath));
    }


    /**
     * 获取hov配置列表
     *
     * @param menuId    菜单id
     * @param classPath 类路径
     * @return List
     */
    @Override
    public List<ClassForm> getHovList(Long menuId, String classPath) {
        return build(ConfigurationBuilder.hovBuild(menuId, classPath));
    }

    private List<ClassForm> build(ConfigurationBuilder configurationBuilder) {
        DynamicClassInfo dynamicClassInfo = DynamicUtil.resolveClass(configurationBuilder.getClassPath(), configurationBuilder.isClassBuildChild());
        return dynamicClassInfo.getFieldInfoList().stream()
                .filter(SuperClassFilter::superFilter)
                .map(
                        f -> {
                            ClassForm.ClassFormBuilder builder = ClassForm.builder();
                            builder.attributeName(f.getAttributeName());
                            builder.attributeNameCh(f.getAttributeNameCh());
                            builder.radio(f.isRadio());
                            if (configurationBuilder.isBuildEnum()) {
                                enumerate(builder, f);
                            }
                            if (configurationBuilder.isBuildHov()) {
                                hov(builder, dynamicClassInfo.getClassPath(), f);
                            }
                            builder.attributeType(f.getType());
                            builder.tableName(f.getTableName());
                            builder.tableColumnName(f.getTableColumnName());
                            return builder.build();
                        }
                ).collect(Collectors.toList());
    }

    /**
     * 枚举操作
     *
     * @param builder 构建对象
     * @param f       属性对象
     */
    private void enumerate(ClassForm.ClassFormBuilder builder, DynamicClassInfo.FieldInfo f) {
        List<SysDictData> dictCache = DictUtils.getDictCache(StringUtils.camelToUnderline(f.getAttributeName()));
        if (dictCache != null && !dictCache.isEmpty()) {
            builder.enumerate(true);
            builder.enumList(dictCache);
        }
    }

    /**
     * hov操作
     *
     * @param builder   构建对象
     * @param classPath 类路径
     * @param f         属性对象
     */
    private void hov(ClassForm.ClassFormBuilder builder, String classPath, DynamicClassInfo.FieldInfo f) {
        ClassBindHov hovCache = ClassBindHovUtils.getHovCache(classPath, f.getAttributeName());
        builder.hov(true);
        builder.hovId(hovCache.getHovId());
        builder.hovBuildColumnName(hovCache.getBuildColumnName());
    }
}
